package com.bow.syntax

/**
  * @author vv
  * @since 2017/7/24.
  */
object ApplyDemo {

  def main(args: Array[String]) {
    // 获取到单例
    val a = ApplyTest() //这里使用object的apply
    a.haveATry
    a() // 这里是class中的apply使用
  }

}

//将默认构造器私有化
class ApplyTest private {
  def apply() = println("apply in class ApplyTest")

  def haveATry: Unit = {
    println("have a try on apply")
  }
}

object ApplyTest {
  // 单例
  var instance: ApplyTest = null


  def apply() = {
    println("init instance")
    if (instance == null) instance = new ApplyTest
    instance
  }
}